//
//  FPBorrowingController.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/2/8.
//

#import "FPBorrowingController.h"
#import "FPBorrowingAndRepaymentCell.h"
#import "FPLoanDetailController.h"
#import "FPBorrowingAndRepaymentModel.h"
#import "FPNoDataController.h"
#import "FPInreviewController.h"
#import "FPRecommonProductController.h"

@interface FPBorrowingController () <UITableViewDelegate , UITableViewDataSource , DZNEmptyDataSetSource , DZNEmptyDataSetDelegate>

@end

@implementation FPBorrowingController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)initUI {
    
    [self setTitle:@"借款记录" style:(XYCustomNavigationStyleClear) hiddenBackBtn:NO];
    
    self.view.backgroundColor = MAIN_VIEW_BACKGROUND_COLOR;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.frame = CGRectMake(0.f, UIDevice.navigationFullHeight, SCREEN_WIDTH, SCREEN_HEIGHT - UIDevice.navigationFullHeight);
    
    self.tableView.backgroundColor = UIColor.clearColor;
    self.tableView.rowHeight = 82.f;
    [self.view addSubview:self.tableView];
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    
    [self getData];
}

- (void)getData {
    
//    [MBManager showLoading];
    [FPBorrowingAndRepaymentModel getLoanListWithType:(LoanListTypeBorrowing) success:^(NSInteger code, NSString * _Nonnull message, NSArray * _Nonnull list) {
        [MBManager hideLoading];
        
        [self.dataSource removeAllObjects];
        
        for (FPBorrowingAndRepaymentModel *model in list) {
            
            [self.dataSource addObject:model];
        }
        
        [self.tableView reloadData];
        
    } failure:^(NSError * _Nonnull error) {
        [self showError:error];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FPBorrowingAndRepaymentCell *cell;
    
    if (cell == nil) {
        
        cell = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(FPBorrowingAndRepaymentCell.class) owner:nil options:nil].lastObject;
    }
    
    FPBorrowingAndRepaymentModel *model = self.dataSource[indexPath.row];
    
    [cell setBorrowingListStatus:model.status.integerValue];
    
    NSString *time = [FPBorrowingAndRepaymentModel timeOfParam:model.createTime];
    NSString *term = [FPBorrowingAndRepaymentModel termOfParam:model.lengthOfMaturity];
    
    cell.titleLab.text = [NSString stringWithFormat:@"借款金额：%@元",model.amount];
    cell.subTitleLab.text = [NSString stringWithFormat:@"借款时间：%@ 丨 %@期",time , term];
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FPBorrowingAndRepaymentModel *model = self.dataSource[indexPath.row];
    
//#pragma mark - 只有审核成功才会跳转
    // 审核成功
    if (model.status.integerValue == FPBorrowingStatusTypeSuccess) {
     
        FPLoanDetailController *vc = [[FPLoanDetailController alloc] init];
        vc.loanID = model.ID;
        [self.navigationController pushViewController:vc animated:YES];
    }
    // 借款审核中
    else if (model.status.integerValue == FPBorrowingStatusTypeInReview) {
        
//        FPNoDataController *vc = [[FPNoDataController alloc] init];
//        vc.type = RecordListTypeLoanInReview;
//        [self.navigationController pushViewController:vc animated:YES];

        FPRecommonProductController *vc = [[FPRecommonProductController alloc] init];
//#warning 银行卡号 金额信息
//        FPInreviewController *vc = [[FPInreviewController alloc] init];
//        
//        /*
//         @property (nonatomic , strong) NSString *bankCardInfo;
//         @property (nonatomic , strong) NSString *bankCardImgInfo;
//         @property (nonatomic , strong) NSString *amountInfo;
//         */
//        vc.amountInfo = model.amount;
//        vc.bankCardID = model.userBankCardId;
        
        [self.navigationController pushViewController:vc animated:YES];
    }
    // 审核失败
    else if (model.status.integerValue == FPBorrowingStatusTypeFaild) {
        
        FPRecommonProductController *vc = [[FPRecommonProductController alloc] init];
//        FPNoDataController *vc = [[FPNoDataController alloc] init];
//        vc.type = RecordListTypeReviewFailure;
        [self.navigationController pushViewController:vc animated:YES];
    }
}


- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    
    return IMG(@"bg_nodata");
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {

    return -100.f;
}

- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
    
    NSString *text;
    
    text = @"暂无借款记录";
    
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
    [NSString stringToAttribute:attStr font:FONT(12.f) range:NSMakeRange(0, text.length)];
    [NSString stringToAttribute:attStr color:[UIColor hexColor:@"#6A6A6A"] range:NSMakeRange(0, text.length)];

    return attStr;
}


@end
